﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

using AzureMRCore.DataTypes;
using AzureMRCore.InputFormat;
using AzureMRCore.MapRed;
using AzureMRCore.OutputFormat;
using AzureMRCore.Partitioners;
using Microsoft.WindowsAzure;

namespace AzureMRCore.Drivers
{
    public abstract class MRDriver
    {
        // 0- false,1- Hybrid, 2-TCP only
        private int _directTCPTransfer = 0;
        public int DirectTCPTranfer
        {
            get { return _directTCPTransfer; }
            set { _directTCPTransfer = value; }
        }

        public abstract string Name { get; }
        public object ConfigOut { get; set; }

        public virtual bool ConfigMapRed(MRConf mrConf, CloudStorageAccount storageAccount, out object configOut)
        {
            configOut = null;
            return true;
        }

        public abstract IMapper GetMapper();

        public virtual IReducer GetReducer()
        {
            return null;
        }

        public virtual IReducer GetCombiner()
        {
            return null;
        }

        public virtual IMerger GetMerger()
        {
            return null;
        }

        public virtual IPartitioner PartitionerType
        {
            get { return new NullPartitioner(); }
        }

        public abstract IInputFormat<K, V> GetInputFormat<K, V>()
            where K : Key;

        public virtual IInputFormat<K, V> GetBCastInputFormat<K, V>()
            where K : Key
        {
            return GetInputFormat<K, V>();
        }

        public abstract IOutputFormat<K, V> GetMapOutputFormat<K, V>()
            where K : Key, new()
            where V : IValue, new();

        public virtual IOutputFormat<K, V> GetReduceOutputFormat<K, V>()
            where K : Key, new()
            where V : IValue, new()
        {
            return null;
        }

        public virtual IOutputFormat<K, V> GetMergeOutputFormat<K, V>()
            where K : Key, new()
            where V : IValue, new()
        {
            return null;
        }

        public virtual PrimitiveConf GetPrimitive(int currentIteration)
        {
            return new PrimitiveConf();
        }
    }

    public class PrimitiveConf
    {
        public bool IsPrimitive { get; set; }
        public bool AddIteration { get; set; }
        public string AppName { get; set; }
        public int Iteration { get; set; }
        public int NumReduceTasks { get; set; }
        public bool IsMerge { get; set; }

        public PrimitiveConf()
        {
            IsPrimitive = false;
        }
    }
}